package com.yu.mall.service;

import java.math.BigDecimal;
import java.util.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import com.yu.mall.entity.*;
import com.yu.mall.enums.PayStatusEnum;
import com.yu.mall.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.CollectionUtils;
import com.alibaba.fastjson.JSONObject;
import com.yu.mall.entity.Bill;
import com.yu.mall.entity.Jdid;
import com.yu.mall.entity.Order;
import com.yu.mall.entity.OrderConfirm;
import com.yu.mall.entity.OrderItem;
import com.yu.mall.entity.Pay;
import com.yu.mall.entity.PayExample;
import com.yu.mall.entity.PostSales;
import com.yu.mall.entity.PostSalesExample;
import com.yu.mall.entity.User;
import com.yu.mall.exception.ServiceException;
import com.yu.mall.mapper.BillMapper;
import com.yu.mall.mapper.JdidMapper;
import com.yu.mall.mapper.OrderConfirmMapper;
import com.yu.mall.mapper.OrderItemMapper;
import com.yu.mall.mapper.OrderMapper;
import com.yu.mall.mapper.PayMapper;
import com.yu.mall.mapper.PostSalesMapper;
import com.yu.mall.mapper.UserMapper;
import com.yu.mall.util.JDUtils;
import com.yu.mall.util.WechatPayUtil;
import com.yu.mall.vo.ResultMessage;
import com.yu.mall.vo.order.OrderChangeVO;
import com.yu.mall.vo.order.OrderCompleteVO;
import com.yu.mall.vo.order.OrderTuiKuanVO;
import com.yu.mall.vo.order.refundWaresInfoVO;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;
import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class OrderConfirmService {

	@Autowired
	private OrderConfirmMapper orderConfirmMapper;

	@Autowired
	private OrderMapper orderMapper;

	@Autowired
	private OrderItemMapper orderItemMapper;

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private BillMapper billMapper;

	@Autowired
	private PostSalesMapper postSalesMapper;

	@Autowired
	private PayMapper payMapper;

	@Autowired
	private JdidMapper jdidMapper;

	@Autowired
	private PayService payService;

	@Autowired
	private OrderConfirmMapper confirmMapper;

	@Autowired
	private TransactionTemplate transactionTemplate;

	public void synOrderConfirm() {
		Example example = new Example(OrderConfirm.class);
		example.createCriteria().andLessThan("num", 3);
		List<OrderConfirm> orderConfirms = orderConfirmMapper.selectByExample(example);
		if (!CollectionUtils.isEmpty(orderConfirms)) {
			for (OrderConfirm orderConfirm : orderConfirms) {
				try {
					String s = JDUtils.confirmOrder(orderConfirm.getJdOrderNo());
					if ("true".equals(s)) {
						orderConfirmMapper.deleteByPrimaryKey(orderConfirm.getId());
					} else {
						orderConfirm.setReason("京东接口返回错误");
						orderConfirm.setNum(orderConfirm.getNum() + 1);
						orderConfirmMapper.updateByPrimaryKeySelective(orderConfirm);
					}
				} catch (Exception e) {
					log.error("确认订单失败：" + orderConfirm.getOrderNo(), e);
					if (e instanceof ServiceException) {
						ServiceException serviceException = (ServiceException) e;
						orderConfirm.setReason(serviceException.getErrorMsg());
					} else {
						orderConfirm.setReason("网络不通");
					}
					orderConfirm.setNum(orderConfirm.getNum() + 1);
					orderConfirmMapper.updateByPrimaryKeySelective(orderConfirm);
				}
			}
		}
	}

	public void synOrderComplete() {
		List<OrderCompleteVO> completeMessage = JDUtils.getCompleteMessage();
		if (!CollectionUtils.isEmpty(completeMessage)) {
			for (OrderCompleteVO orderCompleteVO : completeMessage) {
				boolean flag = transactionTemplate.execute(new TransactionCallback<Boolean>() {
					@Override
					public Boolean doInTransaction(TransactionStatus transactionStatus) {
						try {
							Example example = new Example(Order.class);
							example.createCriteria().andEqualTo("jdOrderNo", orderCompleteVO.getOrderId());
							Order order = new Order();
							order.setJdOrderNo(orderCompleteVO.getOrderId());
							Order order1 = orderMapper.selectOne(order);
							if (order1 != null) {
								if ("2".equals(order1.getStatus())) {
									order.setStatus("3");
									order.setCompleteTime(orderCompleteVO.getCompleteTime());
									order.setUpdateTime(new Date());
									int i = orderMapper.updateByExampleSelective(order, example);
									if (i >= 1) {
										try {
											JDUtils.deleteCompleteMessage(orderCompleteVO.getId());
										} catch (ServiceException e) {
											log.error("删除已完成订单消息失败");
										}
									}
								} else {
									try {
										JDUtils.deleteCompleteMessage(orderCompleteVO.getId());
									} catch (ServiceException e) {
										log.error("删除已完成订单消息失败");
									}
								}
							} else {
								OrderItem orderItem = new OrderItem();
								orderItem.setJdOrderId(orderCompleteVO.getOrderId());
								List<OrderItem> orderItems = orderItemMapper.select(orderItem);
								if (!CollectionUtils.isEmpty(orderItems)) {
									String orderNo = orderItems.get(0).getOrderNo();
									Example orderItemExample = new Example(OrderItem.class);
									orderItemExample.createCriteria().andEqualTo("jdOrderId",
											orderCompleteVO.getOrderId());
									orderItem.setCompleteTime(orderCompleteVO.getCompleteTime());
									orderItemMapper.updateByExampleSelective(orderItem, orderItemExample);

									orderItemExample.clear();
									orderItemExample.createCriteria().andEqualTo("orderNo", orderNo)
											.andIsNull("completeTime");
									List<OrderItem> items = orderItemMapper.selectByExample(orderItemExample);
									if (CollectionUtils.isEmpty(items)) {
										order = new Order();
										order.setStatus("3");
										order.setCompleteTime(orderCompleteVO.getCompleteTime());
										order.setUpdateTime(new Date());
										Example orderExample = new Example(Order.class);
										orderExample.createCriteria().andEqualTo("orderNo", orderNo);
										orderMapper.updateByExampleSelective(order, orderExample);
									}
								}
								try {
									JDUtils.deleteCompleteMessage(orderCompleteVO.getId());
								} catch (ServiceException e) {
									log.error("删除已完成订单消息失败", e);
								}
							}
						} catch (Exception e) {
							transactionStatus.setRollbackOnly();
							log.error("处理已完成订单消息失败,订单id为：" + orderCompleteVO.getOrderId(), e);
						}
						return true;
					}
				});
			}
		}
	}

	@Transactional
	public void synOrderTuiKuan() {
		List<OrderTuiKuanVO> message = JDUtils.getMessage();
		if (!CollectionUtils.isEmpty(message)) {
			for (OrderTuiKuanVO orderTuiKuanVO : message) {
				boolean flag = transactionTemplate.execute(new TransactionCallback<Boolean>() {
					@Override
					public Boolean doInTransaction(TransactionStatus transactionStatus) {
				try {
					Jdid jdid2 = new Jdid();
					jdid2.setJdid(orderTuiKuanVO.getId());
					Jdid selectOne2 = jdidMapper.selectOne(jdid2);
					if (selectOne2 != null) {
						//JDUtils.deleteCompleteMessage(orderTuiKuanVO.getId());
					} else {
						PayExample payExample = new PayExample();
						payExample.createCriteria().andOutRefundNoEqualTo(orderTuiKuanVO.getOutApplyId())
								.andTypeEqualTo("1").andRefundStatusIn(Arrays.asList("ING", "SUCC"));
						List<Pay> pays = payMapper.selectByExample(payExample);

						if (pays.size() == 0) {
							PostSalesExample example = new PostSalesExample();
							example.createCriteria().andJdOrderIdEqualTo(orderTuiKuanVO.getOrderId())
									.andThirdApplyIdEqualTo(orderTuiKuanVO.getOutApplyId());
							List<PostSales> selectByExample = postSalesMapper.selectByExample(example);
							
							if(selectByExample !=null && selectByExample.size()>0) {
								PostSales postSales = postSalesMapper.selectByExample(example).get(0);
								Order order = new Order();
								order.setOrderNo(postSales.getOrderNo());
								Order order1 = orderMapper.selectOne(order);
								BigDecimal allJdPrice = new BigDecimal(orderTuiKuanVO.getRefundAmount());
								BigDecimal num = new BigDecimal(postSales.getNum());
								BigDecimal jdPrice = postSales.getJdPrice();
								BigDecimal allPrice = num.multiply(jdPrice);
								if(order1 !=null) {
									if ("1".equals(order1.getPayType())) {
										log.info(order1.getOrderNo()+"订单号进入余额退款逻辑");
										Example userExample = new Example(User.class);
										Criteria createCriteria = userExample.createCriteria();
										createCriteria.andEqualTo("openid", order1.getOpenid());
										User selectOneByExample = userMapper.selectOneByExample(userExample);
										selectOneByExample.setBalance(selectOneByExample.getBalance().add(allPrice));
										int updateByPrimaryKey = userMapper.updateByPrimaryKeySelective(selectOneByExample);
										if (updateByPrimaryKey > 0) {
											Bill bill = new Bill();
											bill.setAmount(allPrice);
											bill.setJdAmount(allJdPrice);
											bill.setCreateTime(new Date());
											bill.setPayType(order1.getPayType());
											bill.setOrderNo(order1.getOrderNo());
											bill.setJdOrderNo(order1.getJdOrderNo());
											bill.setType("2");
											billMapper.insertSelective(bill);

											Pay pay = new Pay();
											pay.setOrderId(order1.getOrderNo());
											pay.setIsRefund(true);
											pay.setRefundStatus("SUCC");
											pay.setRefundId(bill.getId().toString());
											pay.setRefundTime(new Date());
											pay.setOutRefundNo(orderTuiKuanVO.getOutApplyId());
											pay.setRefundAmount(allPrice.multiply(new BigDecimal(100)).intValue());
											pay.setPayChannel("POINTS");
											pay.setType("1");
											payMapper.insert(pay);
											log.info(bill.getOrderNo()+"订单余额退款成功tktype:2");
										}
										jdidMapper.insert(jdid2);
										//JDUtils.deleteCompleteMessage(orderTuiKuanVO.getId());
									} else if ("2".equals(order1.getPayType())) {
										log.info(order1.getOrderNo()+"订单号进入微信退款逻辑");
										log.info("order1.getOrderNo()"+order1.getOrderNo()+"orderTuiKuanVO.getOutApplyId()"+orderTuiKuanVO.getOutApplyId()+"退款金额"+allPrice.multiply(new BigDecimal(100)).intValue()+"请求");
										ResultMessage<String> refundPay = payService.refundPay(order1.getOrderNo(),
												orderTuiKuanVO.getOutApplyId(),
												allPrice.multiply(new BigDecimal(100)).intValue(), "京礼商城退款");
										log.info("定时查询微信退款返回"+JSONObject.toJSONString(refundPay));
										if (PayStatusEnum.FAIL.getCode().equals(refundPay.getMsg())) {
											ResultMessage.fail("请求微信退款失败");
											log.info("order1.getOrderNo()"+order1.getOrderNo()+"orderTuiKuanVO.getOutApplyId()"+orderTuiKuanVO.getOutApplyId()+"退款金额"+allPrice.multiply(new BigDecimal(100)).intValue()+"请求微信退款失败");
										} else if (PayStatusEnum.SUCC.getCode().equals(refundPay.getMsg())) {
											Bill bill = new Bill();
											bill.setAmount(allPrice);
											bill.setJdAmount(allJdPrice);
											bill.setCreateTime(new Date());
											bill.setPayType(order1.getPayType());
											bill.setOrderNo(order1.getOrderNo());
											bill.setJdOrderNo(order1.getJdOrderNo());
											bill.setType("2");
											bill.setTktype("2");
											billMapper.insertSelective(bill);
											log.info(bill.getOrderNo()+"订单微信退款成功tktype:2");
											jdidMapper.insert(jdid2);
										} else if (PayStatusEnum.ING.getCode().equals(refundPay.getMsg())) {
											PayExample payExample2 = new PayExample();
											payExample2.createCriteria().andOutRefundNoEqualTo(orderTuiKuanVO.getOutApplyId());
											Pay pays2 = payMapper.selectByExample(payExample).get(0);
											Bill bill = new Bill();
											bill.setAmount(allPrice);
											bill.setJdAmount(allJdPrice);
											bill.setCreateTime(new Date());
											bill.setPayType(order1.getPayType());
											bill.setOrderNo(order1.getOrderNo());
											bill.setJdOrderNo(order1.getJdOrderNo());
											bill.setType("2");
											bill.setTktype("1");
											bill.setPayid(pays2.getId());
											billMapper.insertSelective(bill);
											jdidMapper.insert(jdid2);
											log.info(bill.getOrderNo()+"订单微信退款进行中tktype:1");
										}
									}
								}
							}else {
								log.info("PostSales暂未查询到：JdOrderId===="+orderTuiKuanVO.getOrderId()+"    和ThirdApplyId===="+orderTuiKuanVO.getOutApplyId());
							}
						}
					}
				} catch (Exception e) {
					// TODO: handle exception
					transactionStatus.setRollbackOnly();
					log.error("处理退款已完成订单消息失败,订单id为：" + orderTuiKuanVO.getOrderId(), e);
				}
						return true;
					}
                });
			}
		}
	}
	public static void main(String[] args) {
		ResultMessage<String> refundPay = refundPay();
		if (PayStatusEnum.FAIL.getCode().equals(refundPay.getMsg())) {
			System.out.println(1);
		}else if (PayStatusEnum.SUCC.getCode().equals(refundPay.getMsg())) {
			System.out.println(2);
		}else if (PayStatusEnum.ING.getCode().equals(refundPay.getMsg())) {
			System.out.println(2);
		}
		System.out.println(4);
	}
	
	 public static ResultMessage<String> refundPay() {
		 return ResultMessage.success(PayStatusEnum.SUCC.getCode());
	 }

	public void cancelOrderComplete() {
		List<Order> list = orderMapper.cancelOrderComplete();
		if (!CollectionUtils.isEmpty(list)) {
			log.info("需要取消的订单数" + list.size());
			for (Order order : list) {
				try {
					Order order1 = new Order();
					order1.setId(order.getId());
					order1.setStatus("4");
					order1.setUpdateTime(new Date());
					orderMapper.updateByPrimaryKeySelective(order1);
					try {
						JDUtils.cancel(order.getJdOrderNo());
					} catch (Exception e) {
						log.error(order.getOrderNo() + "京东订单取消失败", e);
					}
				} catch (Exception e) {
					log.error(order.getOrderNo() + "订单取消失败", e);
				}
			}
		}

	}

	public void synOrderChange() throws Exception {
		// TODO Auto-generated method stub
		List<OrderChangeVO> changeMessage = JDUtils.getChangeMessage();
		if (!CollectionUtils.isEmpty(changeMessage)) {
			for (OrderChangeVO orderChangeVO : changeMessage) {
				try {
					PostSalesExample example = new PostSalesExample();
					example.createCriteria().andJdOrderIdEqualTo(orderChangeVO.getOrderId())
							.andThirdApplyIdEqualTo(orderChangeVO.getThirdApplyId());
					PostSales postSales = postSalesMapper.selectByExample(example).get(0);
					if(!"".equals(postSales.getApplyStep())&& postSales.getApplyStep() !=null) {
						if (Integer.parseInt(postSales.getApplyStep()) >= Integer.parseInt(orderChangeVO.getApplyStep())) {
							JDUtils.deleteCompleteMessage(orderChangeVO.getId());
							return;
						}
					}
					PostSales updateBean = new PostSales();
					updateBean.setId(postSales.getId());
					updateBean.setApplyStep(orderChangeVO.getApplyStep());
					if ("60".equals(updateBean.getApplyStep())) {
						updateBean.setStatus("2");
					} else if ("70".equals(updateBean.getApplyStep())) {
						updateBean.setStatus("3");
					}
					postSalesMapper.updateByPrimaryKeySelective(updateBean);
					JDUtils.deleteCompleteMessage(orderChangeVO.getId());
				} catch (Exception e) {
					// TODO: handle exception
					log.error("处理申请单环节变更消息失败,订单id为：" + orderChangeVO.getOrderId(), e);
				}
			}
		}
	}

	public void synOrderId() {
		List<OrderCompleteVO> completeMessage = JDUtils.synOrderId();
		if (!CollectionUtils.isEmpty(completeMessage)) {
			for (OrderCompleteVO orderCompleteVO : completeMessage) {
				boolean flag = transactionTemplate.execute(new TransactionCallback<Boolean>() {
	                @Override
	                public Boolean doInTransaction(TransactionStatus transactionStatus) {
				try {
					String s = JDUtils.selectJdOrder(orderCompleteVO.getOrderId());
					JSONObject jsonObject = JSON.parseObject(s);
					JSONArray cOrders = jsonObject.getJSONArray("cOrder");
					// 父订单
					if (CollectionUtils.isEmpty(cOrders)) {
						Order order = new Order();
						order.setJdOrderNo(orderCompleteVO.getOrderId());
						order = orderMapper.selectOne(order);
						Example example = new Example(OrderItem.class);
						example.createCriteria().andEqualTo("orderNo", order.getOrderNo());
						OrderItem orderItem = new OrderItem();
						orderItem.setJdOrderId(orderCompleteVO.getOrderId());
						orderItemMapper.updateByExampleSelective(orderItem, example);
					} else {
						for (int i = 0; i < cOrders.size(); i++) {
							JSONObject cOrder = cOrders.getJSONObject(i);
							String jdOrderId = cOrder.getString("jdOrderId");
							String pOrder = cOrder.getString("pOrder");
							JSONArray sku = cOrder.getJSONArray("sku");
							if (!CollectionUtils.isEmpty(sku)) {
								Order order = new Order();
								order.setJdOrderNo(pOrder);
								Order order1 = orderMapper.selectOne(order);
								if (order1 != null) {
									Example example = new Example(OrderItem.class);
									Criteria criteria = example.createCriteria();
									criteria.andEqualTo("orderNo", order1.getOrderNo());
									ArrayList<String> skuIds = Lists.newArrayList();
									for (int j = 0; j < sku.size(); j++) {
										skuIds.add(sku.getJSONObject(j).getString("skuId"));
									}
									criteria.andIn("skuId", skuIds);
									OrderItem orderItem = new OrderItem();
									orderItem.setJdOrderId(jdOrderId);
									orderItemMapper.updateByExampleSelective(orderItem, example);
								}

							}
						}
					}
					try {
						JDUtils.deleteCompleteMessage(orderCompleteVO.getId());
					} catch (ServiceException e) {
						log.error("删除拆单消息失败", e);
					}
				} catch (ServiceException e) {
					transactionStatus.setRollbackOnly();
					log.error("处理拆单消息失败", e);
				}
				return true;
	                }
	            });
			}
		}

	}

	public void synOrderIdNew() {
		List<Order> orders = orderMapper.selectAll();
		for (Order order : orders) {
			boolean flag = transactionTemplate.execute(new TransactionCallback<Boolean>() {
                @Override
                public Boolean doInTransaction(TransactionStatus transactionStatus) {
			try {
				String s = JDUtils.selectJdOrder(order.getJdOrderNo());
				JSONObject jsonObject = JSON.parseObject(s);
				JSONArray cOrders = jsonObject.getJSONArray("cOrder");
				// 父订单
				if (CollectionUtils.isEmpty(cOrders)) {
					Example example = new Example(OrderItem.class);
					example.createCriteria().andEqualTo("orderNo", order.getOrderNo());
					OrderItem orderItem = new OrderItem();
					orderItem.setJdOrderId(order.getJdOrderNo());
					orderItemMapper.updateByExampleSelective(orderItem, example);
				} else {
					for (int i = 0; i < cOrders.size(); i++) {
						JSONObject cOrder = cOrders.getJSONObject(i);
						String jdOrderId = cOrder.getString("jdOrderId");
						String pOrder = cOrder.getString("pOrder");
						JSONArray sku = cOrder.getJSONArray("sku");
						if (!CollectionUtils.isEmpty(sku)) {
							Order order2 = new Order();
							order2.setJdOrderNo(pOrder);
							Order order1 = orderMapper.selectOne(order2);
							if (order1 != null) {
								Example example = new Example(OrderItem.class);
								Criteria criteria = example.createCriteria();
								criteria.andEqualTo("orderNo", order1.getOrderNo());
								ArrayList<String> skuIds = Lists.newArrayList();
								for (int j = 0; j < sku.size(); j++) {
									skuIds.add(sku.getJSONObject(j).getString("skuId"));
								}
								criteria.andIn("skuId", skuIds);
								OrderItem orderItem = new OrderItem();
								orderItem.setJdOrderId(jdOrderId);
								orderItemMapper.updateByExampleSelective(orderItem, example);
							}
						}
					}
				}
				try {
					// JDUtils.deleteCompleteMessage(orderCompleteVO.getId());
				} catch (ServiceException e) {
					log.error("删除拆单消息失败", e);
				}
			} catch (ServiceException e) {
				transactionStatus.setRollbackOnly();
				log.error("处理拆单消息失败", e);
			}
					return true;
                }
            });
		}
	}

	@Transactional
	public void synReturning() {
		PayExample payExample = new PayExample();
		payExample.createCriteria().andRefundStatusEqualTo("ING");
		List<Pay> pays = payMapper.selectByExample(payExample);
		for (Pay pay : pays) {
			boolean flag = transactionTemplate.execute(new TransactionCallback<Boolean>() {
                @Override
                public Boolean doInTransaction(TransactionStatus transactionStatus) {
			try {
				ResultMessage<String> message = payService.queryRefundPay(pay.getOutTradeNo());
				if (PayStatusEnum.SUCC.getCode().equals(message.getData())) {
					Bill bill = new Bill();
					bill.setTktype("2");
					Example example = new Example(Bill.class);
					example.createCriteria().andEqualTo("payid", pay.getId());
					billMapper.updateByExampleSelective(bill, example);
				}
			} catch (Exception e) {
				transactionStatus.setRollbackOnly();
				log.error("处理退款中同步消息失败", e);
			}
					return true;
                }
            });
		}
	}

	@Transactional
	public void synPaying() {
		PayExample payExample = new PayExample();
		payExample.createCriteria().andStatusEqualTo("ING");
		List<Pay> pays = payMapper.selectByExample(payExample);
		for (Pay pay : pays) {
			boolean flag = transactionTemplate.execute(new TransactionCallback<Boolean>() {
                @Override
                public Boolean doInTransaction(TransactionStatus transactionStatus) {
			try {
				ResultMessage<String> message = payService.queryPay(pay.getOutTradeNo());
				if (PayStatusEnum.SUCC.getCode().equals(message.getMsg())) {
					Example example = new Example(Order.class);
					example.createCriteria().andEqualTo("orderNo", pay.getOrderId());
					Order order = orderMapper.selectOneByExample(example);
					// 调用京东确订单
					boolean flag = true;
					try {
						String s = JDUtils.confirmOrder(order.getJdOrderNo());
						flag = "true".equals(s);
					} catch (Exception e) {
						log.error("确认订单失败：" + order.getOrderNo(), e);
						OrderConfirm confirm = new OrderConfirm();
						confirm.setCreateTime(new Date());
						confirm.setJdOrderNo(order.getJdOrderNo());
						confirm.setNum(0);
						if (e instanceof ServiceException) {
							ServiceException serviceException = (ServiceException) e;
							confirm.setReason(serviceException.getErrorMsg());
						} else {
							confirm.setReason("网络不通");
						}
						confirmMapper.insertSelective(confirm);
						flag = false;
					}
					if (flag) {
						order.setStatus("2");
					} else {
						log.error("确认订单失败：" + order.getOrderNo());
						OrderConfirm confirm = new OrderConfirm();
						confirm.setCreateTime(new Date());
						confirm.setJdOrderNo(order.getJdOrderNo());
						confirm.setNum(0);
						confirm.setReason("京东接口返回错误");
						confirmMapper.insertSelective(confirm);
					}
					order.setUpdateTime(new Date());
					order.setPayType("2");
					order.setPayTime(new Date());
					int i = orderMapper.updateByExampleSelective(order, example);
					Bill bill = new Bill();
					bill.setAmount(order.getOrderPriceTotal());
					bill.setJdAmount(order.getJdOrderPriceTotal());
					bill.setCreateTime(new Date());
					bill.setPayType("2");
					bill.setOrderNo(order.getOrderNo());
					bill.setJdOrderNo(order.getJdOrderNo());
					bill.setType("1");
					billMapper.insertSelective(bill);
				}
			} catch (Exception e) {
				transactionStatus.setRollbackOnly();
				log.error("处理支付中同步消息失败", e);
			}
					return true;
                }
            });
		}
	}
}
